package com.clayecloud.module_report_repair.viewModels;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.clayecloud.lib_base.action.ToastAction;
import com.clayecloud.lib_common.apis.ReportRepairApi;
import com.clayecloud.lib_common.apis.entity.house.ClientHouse;
import com.clayecloud.lib_common.utils.GsonUtil;
import com.clayecloud.lib_common.utils.HttpUtil;
import com.clayecloud.lib_common.utils.LoginUtil;
import com.clayecloud.lib_common.utils.MessageUtil;
import com.clayecloud.module_report_repair.entity.ReportRepair;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ReportRepairListViewModel extends ViewModel implements ToastAction {

    private ReportRepairApi reportRepairApi;

    private MutableLiveData<List<ReportRepair>> reportRepairList;

    public void setReportRepairList(MutableLiveData<List<ReportRepair>> reportRepairList) {
        this.reportRepairList = reportRepairList;
    }

    public MutableLiveData<List<ReportRepair>> getReportRepairList() {
        return reportRepairList;
    }

    public void leadData(){
        reportRepairApi = HttpUtil.getInstance().create(ReportRepairApi.class);
        MutableLiveData<List<ReportRepair>> reportRepairList = new MutableLiveData<>();
        reportRepairList.setValue(new ArrayList<>());
        setReportRepairList(reportRepairList);
        initReportRepairList();
    }

    private void initReportRepairList() {
        reportRepairApi.select(LoginUtil.getUserID())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MessageUtil>() {
                    private Disposable disposable;

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(@NonNull MessageUtil messageUtil) {
                        List<ReportRepair> reportRepairs = GsonUtil.getInstance().fromJson(GsonUtil.getInstance().toJson(messageUtil.getObj()),new TypeToken<List<ReportRepair>>() {}.getType());
                        MutableLiveData<List<ReportRepair>> reportRepairList = getReportRepairList();
                        reportRepairList.setValue(reportRepairs);
                        setReportRepairList(reportRepairList);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        toast("网络请求失败，请联系管理员");
                        Log.e("ReportRepairListViewModel getReportRepairList error",e.getMessage());
                        disposable.dispose();
                    }

                    @Override
                    public void onComplete() {
                        disposable.dispose();
                    }
                });



    }

}
